/* 日历计算相关的工具函数 */
import dayjs from 'dayjs'

// 获取当前年月日
export const getYearMonthDay =  (date = dayjs(), type = 'string') => {
  return type === 'string' 
    ? date.format('YYYY-MM-DD') 
    : { 
        year: date.format('YYYY') , 
        month: date.format('MM'), 
        date: date.format('DD')
    }
}

// 当前月日历面板日期列表
export const getDayInMonthList = (yearMonth = dayjs().format('YYYY-MM')) => {
  let res = []

  // 1. 获取当前月有多少天 daysInMonth()
  const daysNum = dayjs(yearMonth).daysInMonth()
  for (let i = 1; i <= daysNum; i++ ) {
    res.push(`${yearMonth}-${i > 9 ? i : '0' + i}`)
  }

  // 2. 填补上一月数据
  const firstDay = res[0]
  const preMonthfillLen = dayjs(firstDay).get('day') - 1

  for (let i = 1; i < preMonthfillLen + 1; i++) {
    res.unshift(dayjs(firstDay).subtract(i, 'day').format('YYYY-MM-DD'))
  }

  // 3. 填补下一月数据
  const lastDay = res.slice(-1)
  const nextMonthfillLen = 42 - res.length
  for (let i = 1; i < nextMonthfillLen + 1; i++) {
    res.push(dayjs(lastDay).add(i, 'day').format('YYYY-MM-DD'))
  }

  return res 
}
